/*
 * @Author: 陶奇林 1624362469@qq.com
 * @Date: 2023-10-25 13:48:25
 * @LastEditors: 陶奇林 1624362469@qq.com
 * @LastEditTime: 2023-11-08 15:06:35
 * @FilePath: \tql-midway\src\configuration.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Configuration, App } from '@midwayjs/core';
import * as koa from '@midwayjs/koa';
import * as validate from '@midwayjs/validate';
import * as info from '@midwayjs/info';
import { join } from 'path';
import { DefaultErrorFilter } from './filter/default.filter';
import { NotFoundFilter } from './filter/notfound.filter';
import { ReportMiddleware } from './middleware/report.middleware';
import * as orm from '@midwayjs/typeorm';
import { FormatMiddleware } from './middleware/format.middleware';
import * as jwt from '@midwayjs/jwt';
import * as redis from '@midwayjs/redis';
import { SecurityMiddleware } from './middleware/security.middleware';
import * as swagger from '@midwayjs/swagger';
import * as dotenv from 'dotenv';
import * as staticFile from '@midwayjs/static-file';

// 初始化环境变量
dotenv.config();

@Configuration({
  imports: [
    koa,
    validate,
    {
      component: info,
      enabledEnvironment: ['local'],
    },
    orm, // 加载 typeorm 组件
    jwt,
    redis,
    swagger,
    staticFile,
  ],
  importConfigs: [join(__dirname, './config')],
})
export class MainConfiguration {
  @App('koa')
  app: koa.Application;

  /**
   * 在依赖注入容器 ready 的时候执行
   */
  async onReady() {
    // add middleware
    this.app.useMiddleware([SecurityMiddleware, FormatMiddleware, ReportMiddleware]);
    // add filter
    this.app.useFilter([NotFoundFilter, DefaultErrorFilter]);
  }

  /**
   * 在应用配置加载后执行
   */
  async onConfigLoad() {}

  /**
   * 在应用服务启动后执行
   */
  async onServerReady() {}

  /**
   * 在应用停止的时候执行
   */
  async onStop() {}
}
